<template>
  <view>
    <!-- 搜索 -->
    <my-search @click="gotoSearch"></my-search>
    
    <!-- 分类导航 -->
    <view class="cate-box">
      <!-- 左侧以及导航 -->
      <scroll-view class="scroll-left" scroll-y="true" :style="{height: wh + 'px'}">
        <view :class="['scroll-left-item', i === selectActive ? 'active' : '']" v-for="(item, i) in cateList" :key="i" @click="changeActive(i)">{{item.cat_name}}</view>
      </scroll-view>
      
      <!-- 右侧二级导航 -->
      <scroll-view class="scroll-right" scroll-y="true" :style="{height: wh + 'px'}" :scroll-top="scrollTop">
        <view class="scroll-right-item" v-for="(item, i) in cateLv2" :key="i">
          <!-- 二级分类title -->
          <view class="right-item-title">
            /{{item.cat_name}}/
          </view>
          
          <!-- 二级分类详情 -->
          <view class="right-itrm-detail">
            <view class="detail-item" v-for="(item2,i2) in item.children" :key="i2" @click="gotoGoodsList(item2)">
              <!-- 上面图片 -->
              <image :src="item2.cat_icon" mode="widthFix"></image>
              
              <!-- 下面文字 -->
              <text>{{item2.cat_name}}</text>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
 import setTabBar from '@/mixin/setTabBar.js'
 export default {
   mixins: [setTabBar],
    data() {
      return {
        wh: 0, // 页面状态
        selectActive: 0, // 选中的一级分类index
        scrollTop: 0, // 纵向滚动条距离顶部位置
        cateList: [], // 分类数据
        cateLv2: [] // 二级分类数据
      };
    },
    
    onLoad() {
      // 动态获取屏幕可用高度
      const sysInfo = uni.getSystemInfoSync()
      this.wh = sysInfo.windowHeight - 50
      
      // 发请求
      this.getCateList()
    },
    
    methods: {
      // 获取分类数据
      async getCateList() {
        const {data: res} = await uni.$http.get('/categories')
        
        // 获取失败
        if(res.meta.status !== 200) return uni.$showMsg()
        
        // 获取成功 - 数据赋值
        this.cateList = res.message
        
        // 二级分类数据
        this.cateLv2 = res.message[0].children
        
      },
      
      // 点击一级分类标签
      changeActive(i) {
        // 改变一级分类索引
        this.selectActive = i
        
        // 修改二级分类数据
        this.cateLv2 = this.cateList[i].children
        
        // 动态设置ScrollBar 到顶部的距离
        this.scrollTop = Math.random()
      },
      
      // 点击商品, 跳转到商品列表页
      gotoGoodsList(goods) {
        uni.navigateTo({
          url: `/subpkg/goods_list/goods_list?cid=${goods.cat_id}`
        })
      },
      
      // 点击搜索, 跳转到搜索页
      gotoSearch() {
        uni.navigateTo({
          url: '/subpkg/search/search'
        })
      }
      
    }
    
    
  }
</script>

<style lang="scss">
.cate-box {
  display: flex;
  
  .scroll-left {
    width: 240rpx;
    .scroll-left-item {
      height: 120rpx;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 26rpx;
      &.active {
        background-color: #fff;
        display: flex;
        justify-content: space-between;
        
        &::before {
          content: '';
          width: 4rpx;
          height: 120rpx;
          background-color: #c00000;
        }
        &::after {
          content: '';
          width: 4rpx;
          height: 120rpx;
          background-color: #fff;
        }
      }
    }
  }
  
  .scroll-right {
    background-color: #fff;
    
    .scroll-right-item {
      .right-item-title{
        height: 80rpx;
        display: flex;
        justify-content: center;
        align-items: center;
        font-weight: bold;
      }
      
      .right-itrm-detail {
        display: flex;
        flex-wrap: wrap;
        
        .detail-item {
          width: 33.33%;
          padding: 30rpx;
          box-sizing: border-box;
          text-align: center;
          
          image {
            width: 100%;
          }
          
          text {
            font-size: 24rpx;
          }
          
        }
      }
    }
  }
}
</style>
